1542B - Plus and Multiply - CodeForces Solution


constructive algorithms math number theory *1500

Please click on ads to support us..

Python Code:

t=(int)(input())
for i in range(t):
    w=input().split()
    n=(int)(w[0])
    a=(int)(w[1])
    b=(int)(w[2])
    if a==1 :
    	if (n-1)%b==0 :
    		print("Yes")
    	else :
    		print("No")
    	
    else :
    	t=1
    	flag=0
    	while t<=n :
    		if t%b==n%b:
    			flag=1
    			break
    		t=t*a
    	if flag==1:
    		print("Yes")
    	else :
    		print("No")

C++ Code:

#include <bits/stdc++.h>

#define ll long long
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define ve vector<ll>
#define f(i, x, n) for (ll i = x; i < n; i++)
#define f1(i, x, n) for (ll i = x; i <= n; i++)
#define fm(i, n) for (ll i = n; i > 0; i--)
#define fm1(i, n) for (ll i = n - 1; i >= 0; i--)
#define pi pair<ll, ll>
#define fast ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define el <<"\n"
#define rall(arr) (arr).rbegin(), (arr).rend()

using namespace std;

int xd[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int yd[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int N = 1e9 + 7;



void run()
{
    ll n, a, b, x = 1;cin >> n >> a >> b;
    if (a == 1)
    {
        cout << ((n - 1) % b ? "No" : "Yes") el;
        return;
    }
    while (x <= n)
    {
        if ((n-x)%b==0)
        {
            cout << "Yes" el;
            return;
        }
        x *= a;
    }
    cout << "No" el;
}

int main()
{
    setprecision(10);
    fast;
#ifndef ONLINE_JUDGE
    freopen(R"(D:\proplem\in.txt.txt)", "r", stdin);
#endif
   
    int t = 1;cin >> t;
    while (t--)
        run();
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa